package com.hulman.oms.web;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.hulman.oms.bean.ElectricReport;
import com.hulman.oms.bean.ElectricReportItem;
import com.hulman.oms.bean.Result;
import com.hulman.oms.service.ElectricReportItemService;
import com.hulman.oms.service.ElectricReportService;
import com.hulman.oms.util.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: maxwellens
 */
@RestController
public class ElectricReportController
{
    @Autowired
    private ElectricReportService electricReportService;
    @Autowired
    private HttpServletResponse response;
    @Value("${file-home}")
    private String fileHome;

    @GetMapping("/electric-reports/{id}")
    public Result findElectricReportById(@PathVariable Integer id)
    {
        ElectricReport electricReport = electricReportService.findElectricReportById(id);
        return new Result(electricReport);
    }

    @GetMapping("/electric-reports")
    public Result findElectricReports(Integer page, Integer limit, Integer year, Integer week)
    {
        Map<String, Object> map = new HashMap<>();
        map.put("page", page);
        map.put("limit", limit);
        map.put("year", year);
        map.put("week", week);
        return electricReportService.findElectricReportsResult(map);
    }

    @DeleteMapping("/electric-reports/{ids}")
    public Result deleteElectricReportById(@PathVariable String ids)
    {
        int[] idArrays = NumberUtil.parseIntArray(ids);
        for (Integer id : idArrays)
        {
            electricReportService.deleteElectricReportById(id);
        }
        return Result.SUCCESS;
    }

    @PutMapping("/electric-reports")
    public Result saveElectricReport(ElectricReport electricReport)
    {
        electricReportService.saveElectricReport(electricReport);
        return Result.SUCCESS;
    }

    /**
     * 在线预览
     *
     * @param id
     * @throws IOException
     */
    @GetMapping("/electric-reports/preview")
    public Result previewElectricReports(Integer id) throws IOException
    {
        String fileName = "tmp_" + UUID.randomUUID() + ".xlsx";
        Path filePath = Paths.get(fileHome, fileName);
        OutputStream outputStream = Files.newOutputStream(filePath);
        electricReportService.exportElectricReports(id, outputStream);
        return new Result(fileName);
    }

    @GetMapping("/electric-reports/export")
    public void exportElectricReports(Integer id) throws IOException
    {
        response.setContentType("multipart/form-data");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-disposition", "attachment;filename=electric-report.xlsx");
        electricReportService.exportElectricReports(id, response.getOutputStream());
    }

}
